//
//  WDCardZDYManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/7.
//

#import "WDCardZDYManager.h"
#import "WDCardModel.h"
#import "WDCardManager.h"
#import "WDNativeManager.h"
#import "WDBMSModel.h"
#import "WDNSLog.h"
#import "WDCardView.h"
#import "WDAdStatusManager.h"

@interface WDCardZDYManager ()
@property (nonatomic, strong)WDCardView *cview;
@property (nonatomic, strong)NSMutableArray *customCardModels;
@property (nonatomic, assign)NSInteger count;
@end

@implementation WDCardZDYManager
static WDCardZDYManager *_cardZDYManager = nil;
+ (instancetype)shareCardZDYManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _cardZDYManager = [[self alloc] init];
        _cardZDYManager.customCardModels = [NSMutableArray array];
        _cardZDYManager.count = 0;
    });
    return _cardZDYManager;
}

- (WDCustomCardModel *)getCustomCardModel
{
    if (self.customCardModels.count > 0)
    {
        if (self.count > self.customCardModels.count - 1)
        {
            self.count = 0;
        }
        WDCustomCardModel *model = self.customCardModels[self.count];
        self.count++;
        return model;
    }
    return nil;
}

- (void)loadZDYCard
{
    if (![WDCardModel shareCardModel].hasCardContent)
    {
        [[WDAdStatusManager shareStatusManager] adRequestWithEventName:CARD_EVENT_NAME];
        [WDCardManager WDGetCardJsonWithAppName:@"gwxcz_Google" completionHandler:^(BOOL isSuccess, WDCardModel * _Nonnull model) {
            if (isSuccess)
            {
                [self sortCustomCardModels:[self bubblingArray:model.adCustomList]];
            }
            else{
//                [self loadZDYCard];
            }
            
        }];
    }else
    {
//        [[WDNativeManager shareNative] showNativeAd:YES isJsonSource:YES];
    }
}

- (void)closeZDYCard
{
    
}

- (void)hideCardView
{
    [self.cview hideAdView];
    self.cview = nil;
}

- (void)showCardViewIsCustom:(BOOL)custom
{
    
    UIViewController *rootViewController = [[[UIApplication sharedApplication].windows objectAtIndex:0] rootViewController];
    CGFloat safeAreaTop = 0.0;
    CGFloat safeAreaBottom = 0.0;
    if (@available(iOS 11.0, *)) {
        safeAreaTop = rootViewController.view.safeAreaInsets.top;
        safeAreaBottom = rootViewController.view.safeAreaInsets.bottom;
    }
    
    WDCardModel *model = [WDCardModel shareCardModel];
    CGFloat view_x = model.leftMargin;
    CGFloat view_width = rootViewController.view.frame.size.width-model.rightMargin-model.leftMargin;;
    CGFloat view_height = 350.0;
    if (model.size == SIZE300X250)
    {
        view_height = view_width*(250/300.0);
    }
    else if (model.size == SIZE640X360)
    {
        view_height = view_width*(360/640.0);
    }
    WDLog(@"card view width:%lf height:%lf",view_width,view_height);
    CGFloat view_y = rootViewController.view.frame.size.height-view_height-safeAreaBottom-model.bottomMargin;
    
    self.cview = [[WDCardView alloc] initWithFrame:CGRectMake(view_x, view_y, view_width, view_height) isCustom:custom];
    self.cview.backgroundColor = [UIColor whiteColor];
    [rootViewController.view addSubview:self.cview];
}

- (void)sortCustomCardModels:(NSMutableArray *)models
{
    if (models.count > 0)
    {
        
        NSArray *weights = [WDBMSModel shareBMSModel].promotionWeight;
        if (weights.count > 0)
        {
            for (NSArray *array in weights)
            {
                if (array.count > 1) {
                    NSString *packName = array[1];
                    for (NSDictionary *dict in models)
                    {
                        if ([packName isEqualToString:dict[@"packName"]])
                        {
                            WDCustomCardModel *model = [[WDCustomCardModel alloc] init];
                            model.adDescription = dict[@"adDescription"];
                            model.adTitle = dict[@"adTitle"];
                            model.appName = dict[@"appName"];
                            model.deepLink = dict[@"deepLink"];
                            model.packName = dict[@"packName"];
                            model.videoUrl = dict[@"videoUrl"];
                            model.weight = dict[@"weight"];
                            model.bannerUrl = dict[@"bannerUrl"];
                            model.appIcon = dict[@"appIconUrl"];
                            NSString *ra = dict[@"rating"];
                            model.rating = [self imageNameForStars:[ra intValue]];
                            NSData *bannerImageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:dict[@"bannerUrl"]]];
                            model.bannerImage = [UIImage imageWithData:bannerImageData];
                            
                            NSData *appIconData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:dict[@"appIconUrl"]]];
                            model.appIcon = [UIImage imageWithData:appIconData];
                            [self.customCardModels addObject:model];
                            
                        }
                    }
                }else{
                    for (NSDictionary *dict in models)
                    {
                        WDCustomCardModel *model = [[WDCustomCardModel alloc] init];
                        model.adDescription = dict[@"adDescription"];
                        model.adTitle = dict[@"adTitle"];
                        model.appName = dict[@"appName"];
                        model.deepLink = dict[@"deepLink"];
                        model.packName = dict[@"packName"];
                        model.videoUrl = dict[@"videoUrl"];
                        model.weight = dict[@"weight"];
                        model.bannerUrl = dict[@"bannerUrl"];
                        model.appIcon = dict[@"appIconUrl"];
                        NSString *ra = dict[@"rating"];
                        model.rating = [self imageNameForStars:[ra intValue]];
                        NSData *bannerImageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:dict[@"bannerUrl"]]];
                        model.bannerImage = [UIImage imageWithData:bannerImageData];
                        
                        NSData *appIconData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:dict[@"appIconUrl"]]];
                        model.appIcon = [UIImage imageWithData:appIconData];
                        [self.customCardModels addObject:model];
                    }
                }
                
            }
        }
        else
        {
            for (NSDictionary *dict in models)
            {
                WDCustomCardModel *model = [[WDCustomCardModel alloc] init];
                model.adDescription = dict[@"adDescription"];
                model.adTitle = dict[@"adTitle"];
                model.appName = dict[@"appName"];
                model.deepLink = dict[@"deepLink"];
                model.packName = dict[@"packName"];
                model.videoUrl = dict[@"videoUrl"];
                model.weight = dict[@"weight"];
                model.bannerUrl = dict[@"bannerUrl"];
                model.appIcon = dict[@"appIconUrl"];
                NSString *ra = dict[@"rating"];
                model.rating = [self imageNameForStars:[ra intValue]];
                NSData *bannerImageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:dict[@"bannerUrl"]]];
                model.bannerImage = [UIImage imageWithData:bannerImageData];
                
                NSData *appIconData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:dict[@"appIconUrl"]]];
                model.appIcon = [UIImage imageWithData:appIconData];
                [self.customCardModels addObject:model];
            }
        }
        
        WDLog(@"sortCustomCardModels===%@",self.customCardModels);
    }
}

- (NSString *)imageNameForStars:(int)starRating {
  if (starRating >= 5) {
    return @"stars_5";
  } else if (starRating >= 4.5) {
    return @"stars_4_5";
  } else if (starRating >= 4) {
    return @"stars_4";
  } else if (starRating >= 3.5) {
    return @"stars_3_5";
  }
    return @"";
}

- (NSMutableArray *)bubblingArray:(NSArray *)dataArray
{
    NSMutableArray *temparray = [NSMutableArray array];
    [temparray addObjectsFromArray:dataArray];
    for (int i = 0; i < temparray.count - 1; i++) {
        for (int j = 0; j < temparray.count - 1; j++) {
            NSDictionary *temp1 = temparray[j];
            NSDictionary *temp2 = temparray[j+1];
            float a = [temp1[@"weight"] intValue];
            float b = [temp2[@"weight"] intValue];
            if (a < b)
            {
                NSDictionary *c = temparray[j];
                temparray[j] =  temparray[j+1];
                temparray [j+1] = c;
            }
        }
    }
    WDLog(@"排序后的数组===%@",temparray);
    return temparray;
}

@end
